------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    血梅香 debuff

]]--
------------------------------------------------------------------------------------------------------------------------------------------------
---
    local assets = {
        Asset("ANIM", "anim/hutao_debuff_blood_plum_scent.zip"), 
    }
------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function damage_fn(debuff_inst,player,target,damage_hit)
        if player.components.hutao_com_fire_damage == nil then
            return
        end
        damage_hit = damage_hit or 1
        local damage = 21*math.pow(damage_hit,0.5)
        SpawnPrefab("hutao_sfx_pink_flame"):PushEvent("Set",{target = target})
        player.components.hutao_com_fire_damage:DoFireDamage(target,damage)

    end
------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function OnAttached(inst,target)
        inst.entity:SetParent(target.entity)
        -- inst.Network:SetClassifiedTarget(target)
        inst.Transform:SetPosition(0,0,0)
        -----------------------------------------------------
        ---
            inst.components.hutao_com_blood_plum_scent_for_debuff:SetTarget(target)
            inst.components.hutao_com_blood_plum_scent_for_debuff:SetDamageFn(damage_fn)
        -----------------------------------------------------
        --- 灯光
            local light_fx = target:SpawnChild("minerhatlight")
            light_fx.Light:SetColour(255 / 255, 51 / 255, 153 / 255)
            inst:ListenForEvent("onremove", function()
                light_fx:Remove()
            end)
        -----------------------------------------------------
    end
------------------------------------------------------------------------------------------------------------------------------------------------
--- 指示器特效
    local function fx_periodic_task(inst,player,target,debuff_inst)
        local scale = debuff_inst.replica.hutao_com_blood_plum_scent_for_debuff:GetDamageTimeScale() + 0.2
        scale = scale*2/3
        inst.AnimState:SetScale(scale,scale,scale)
    end
    local function create_fx_task(parent)
        if parent.client_fx then
            parent.__fx_task:Cancel()
        else
            local parent_replica = parent.replica.hutao_com_blood_plum_scent_for_debuff or parent.replica._.hutao_com_blood_plum_scent_for_debuff
            if parent_replica == nil or ThePlayer == nil or ThePlayer.HUD == nil then
                return
            end
            local player = parent_replica:GetPlayer()
            local target = parent_replica:GetTarget()
            if player == nil or target == nil then
                return
            end
            if player ~= ThePlayer then
                parent.__fx_task:Cancel()
                return
            end
            --------------------------------------------------------------
            --- 创建地面特效
                local inst = CreateEntity()
                inst.entity:SetCanSleep(false)
                inst.entity:AddTransform()
                inst.entity:AddAnimState()
                inst.AnimState:SetBank("hutao_debuff_blood_plum_scent")
                inst.AnimState:SetBuild("hutao_debuff_blood_plum_scent")
                inst.AnimState:PlayAnimation("ring",true)
                -- inst.AnimState:SetAddColour(0, .2, .5, 0)
                inst.AnimState:SetLightOverride(1)
                inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
                inst.AnimState:SetLayer(LAYER_WORLD_BACKGROUND)
                inst.AnimState:SetSortOrder(3)
                inst.Transform:SetRotation(math.random(45))
            --------------------------------------------------------------
            ---
                for i = 1, 8, 1 do
                    local fx = CreateEntity()
                    fx.entity:AddTransform()
                    fx.entity:AddAnimState()
                    fx.entity:SetParent(inst.entity)
                    fx.entity:AddFollower()
                    fx.Follower:FollowSymbol(inst.GUID, "slot_"..i,0,0,0,true)
                    fx.AnimState:SetBank("hutao_debuff_blood_plum_scent")
                    fx.AnimState:SetBuild("hutao_debuff_blood_plum_scent")
                    fx.AnimState:PlayAnimation("flower",true)
                end
            --------------------------------------------------------------
            --- 绑定特效
                target:AddChild(inst)
                parent:ListenForEvent("onremove",function()
                    inst:Remove()
                end)
                parent.client_fx = inst
            --------------------------------------------------------------
            --- 特效数据更新
                -- inst:DoPeriodicTask(FRAMES,fx_periodic_task,FRAMES,player,target,parent)
                parent:ListenForEvent("blood_plum_scent_data_update",function()
                    fx_periodic_task(inst,player,target,parent)
                end)
            --------------------------------------------------------------
            --- 创建秒数显示
                local Widget = require "widgets/widget"                
                local Text = require "widgets/text"
                local front_root = ThePlayer.HUD
                local root = front_root:AddChild(Widget())
                root:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
                root:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
                root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
                root.inst:ListenForEvent("onremove",function()
                    root:Kill()
                end,parent)
            --------------------------------------------------------------
            --- txt
                local txt_box = root:AddChild(Text(CODEFONT,30,"10.0",{  255/255 , 51/255 ,153/255 , 1}))
                local offset_y = -50
                TheInput:Hutao_Add_Update_Custom_Fn(parent,function()
                    --------------------------------------------------
                    --- 坐标控制
                        local x,y,z = target.Transform:GetWorldPosition()
                        local s_pt_x,s_pt_y= TheSim:GetScreenPos(x,y,z) -- 左下角为原点。
                        -- print("player in screen",s_pt_x,s_pt_y)
                        root:SetPosition(s_pt_x,s_pt_y+offset_y,0)
                    --------------------------------------------------
                    --- 数据刷新，使用format 强制保留一位小数
                        local remain_time =  parent_replica:GetRemainTime()
                        local damage_time = parent_replica:GetDamageTime()
                        txt_box:SetString("󰀘"..string.format("%.1f",remain_time).."\n󰀈"..string.format("%.1f",damage_time))
                    --------------------------------------------------
                end)
            --------------------------------------------------------------
            ---
                parent:ListenForEvent("entitysleep",function()
                    root:Hide()
                end)
                parent:ListenForEvent("entitywake",function()
                    root:Show()
                end)
            --------------------------------------------------------------

        end
    end
    local function create_fx(parent)
        parent.__fx_task = parent:DoPeriodicTask(FRAMES*5,create_fx_task)
    end
------------------------------------------------------------------------------------------------------------------------------------------------

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()
    inst.entity:AddTransform()

    inst:AddTag("CLASSIFIED")

    inst.entity:SetPristine()
    --- 指示器特效
    if not TheNet:IsDedicated() and TUNING["hutao.Config"].DEBUFF_BLOOD_PLUM_SCENT_INDICATOR then
        create_fx(inst)
    end
    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("hutao_com_blood_plum_scent_for_debuff")
    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)

    return inst
end

return Prefab("hutao_debuff_blood_plum_scent", fn,assets)
